<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>我的订单</p>
    </header>

    <!-- 未支付订单列表部分 -->
    <h3>未支付订单信息：</h3>
    <ul class="order">
      <li v-for="(order, orderIndex) in unpaidOrders" :key="orderIndex">
        <div class="order-info">
          <p>
            {{ getBusinessInfoByOrderId(order.orderId).business? getBusinessInfoByOrderId(order.orderId).business.businessName : 'aa' }}
            <i class="fa fa-caret-down"></i>
          </p>
          <div class="order-info-right">
            <p>¥{{ order.toatal }}</p>
            <div class="order-info-right-icon" @click="goToPay(order.orderId)">去支付</div>
          </div>
        </div>
        <ul class="order-detailet">
          <li v-for="(food, foodIndex) in getFoodsByOrderId(order.orderId)" :key="foodIndex">
            <p>{{ food.food.foodName }} x {{ food.quantity }}</p>
            <p>¥{{ food.food.foodPrice }}</p>
          </li>
          <li>
            <p>配送费</p>
            <p>¥{{ getBusinessInfoByOrderId(order.orderId).business? this.getBusinessInfoByOrderId(order.orderId).business.deliveryPrice : "ss" }}</p>
          </li>
        </ul>
      </li>
    </ul>

    <!-- 已支付订单列表部分 -->
    <h3>已支付订单信息：</h3>
    <ul class="order">
      <li v-for="(order, orderIndex) in paidOrders" :key="orderIndex">
        <div class="order-info">
          <p>
            {{ getBusinessInfoByOrderId(order.orderId).business? getBusinessInfoByOrderId(order.orderId).business.businessName : 'aa' }}
            <i class="fa fa-caret-down"></i>
          </p>
          <div class="order-info-right">
            <p>¥{{ order.toatal }}</p>
          </div>
        </div>
        <ul class="order-detailet">
          <li v-for="(food, foodIndex) in getFoodsByOrderId(order.orderId)" :key="foodIndex">
            <p>{{ food.food.foodName }} x {{ food.quantity }}</p>
            <p>¥{{ food.food.foodPrice }}</p>
          </li>
          <li>
            <p>配送费</p>
            <p>¥{{ getBusinessInfoByOrderId(order.orderId).business? this.getBusinessInfoByOrderId(order.orderId).business.deliveryPrice : "ss" }}</p>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: JSON.parse(sessionStorage.getItem("user")), //当前登录的用户信息
      businessInfo: [], //商家信息（商家名称、配送费）
      orders: [], // 初始化为空数组
      foods: [] //订单中食物详情（食品名称、数量、视频单价）
    };
  },

  computed: {
    unpaidOrders() {
      return this.orders.filter(order => order.status === 0);
    },
    paidOrders() {
      return this.orders.filter(order => order.status === 1);
    }
  },

  mounted() {
    //根据登陆的id查询订单总价
    this.getTotal();
    //根据登陆的id查询商家信息（商家名称、配送费）
    this.getBusiness();
    //根据登陆的id查询食物详情（食品名称、数量、视频单价）
    this.getFood();
  },
  methods: {
    async getTotal() {
      const response = await this.$http.get("/orders/listOrder", {
        params: {
          userId: this.user.userId
        }
      });
      this.orders = response.data.object;
    },
    async getBusiness() {
      const response = await this.$http.get("/orders/listOrderByID", {
        params: {
          userId: this.user.userId
        }
      });
      this.businessInfo = response.data.object;
    },
    async getFood() {
      const response = await this.$http.get("/orders/getOrderItemsByUserId", {
        params: {
          userId: this.user.userId
        }
      });
      this.foods = response.data.object;
    },
    // 根据订单ID获取商家相关信息（商家名称和配送费）
    getBusinessInfoByOrderId(orderId) {
      // 假设businessInfo是一个包含商家信息的数组，根据订单ID找到对应的商家信息并返回
      const business = this.businessInfo.find(b => b.orderId === orderId);
      // console.log(business.business)
      return business || { businessName: "", deliveryPrice: 0 };
    },
    // 根据订单ID获取食物详情数组
    getFoodsByOrderId(orderId) {
      // 假设foods是一个包含所有食物详情的数组，根据订单ID筛选出对应订单的食物详情并返回
      return this.foods.filter(f => f.orderId === orderId);
    },
    goToPay(orderid) {
      // 这里添加跳转到支付页面的逻辑，例如使用Vue Router进行页面导航
      this.$router.push({ path: "/pay", query: { orderId: orderid } });
    }
  }
}
</script>
 
 
 
<style scoped>
/****************** 总容器 ******************/
.wrapper {
  width: 100%;
  max-height: 90vh; 
  overflow-y: auto; /* 添加垂直滚动条，以便在内容超出时可以滚动查看 */
}
/****************** header部分 ******************/
.wrapper header {
  width: 100%;
  height: 12vw;
  background-color: #0097ff;
  color: #fff;
  font-size: 4.8vw;
 
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
 
  display: flex;
  justify-content: center;
  align-items: center;
}
 
/****************** 历史订单列表部分 ******************/
.wrapper h3 {
  margin-top: 12vw;
  box-sizing: border-box;
  padding: 4vw;
  font-size: 4vw;
  font-weight: 300;
  color: #999;
}
.wrapper .order {
  width: 100%;
}
.wrapper .order li {
  width: 100%;
}
.wrapper .order li .order-info {
  box-sizing: border-box;
  padding: 2vw 4vw;
  font-size: 4vw;
  color: #666;
 
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.wrapper .order li .order-info .order-info-right {
  display: flex;
}
.wrapper .order li .order-info .order-info-right .order-info-right-icon {
  background-color: #f90;
  color: #fff;
  border-radius: 3px;
  margin-left: 2vw;
  user-select: none;
  cursor: pointer;
}
 
.wrapper .order li .order-detailet {
  width: 100%;
}
.wrapper .order li .order-detailet li {
  width: 100%;
  box-sizing: border-box;
  padding: 1vw 4vw;
  color: #666;
  font-size: 3vw;
 
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>